import Scanner from './Scanner'
import nestTokens from './nestTokens';
/**
 * 将模板字符串变为tokens数组
*/
export default function parseTemplateToTokens(templateStr){
    var tokens = []
    var scanner = new Scanner(templateStr)
    var words;
    while(!scanner.eos()){
        words = scanner.scanUtil("{{")
        if(words.length>0){
            let isInJJH = false
            var _words = ''
            //去空格
            for(let i = 0; i < words.length; i++){
                if(words[i] != ' '){
                    if (words[i] == '<'){
                        isInJJH = true
                    }else if(words[i] == '>'){
                        isInJJH = false
                    }
                    _words += words[i]
                }else{
                    if (isInJJH) {
                        _words += words[i]
                    }
                }
            }
            tokens.push(['text', _words])
        }
        scanner.scan("{{")
        words = scanner.scanUtil("}}")
        if(words.length>0){
            if(words[0]=='#'){
                tokens.push(['#',words.substring(1)])
            }else if(words[0]=='/'){
                tokens.push(['/',words.substring(1)])
            }else{
                tokens.push(['name',words])
            }
        }
        scanner.scan("}}")
    }
    return nestTokens(tokens)
}